
/*
 * GET users listing.
 */

var jade = require('jade');
var util = require('util');

exports.list = function(req, res){
    res.send("respond with a resource");
};

exports.showTables = function (req, res) {
    var server = req.param('server');
    console.log(server);

    if (server == "Test") {
        console.log("Test");

        var DBPool = req.app.get('Test_Game_Master');

        DBPool.getConnection(function (err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("show Tables;");

            conn.query(strQuery, function (err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                } else {
                    console.log(rows);
                    res.render('jjaehuny/showTables', { type: "TEST SERVER", server: server, rows: rows, fields: fields });
                }
            })
        })
    }
    else {
        console.log("Real");

        var DBPool = req.app.get('Real_Game_Backup');

        DBPool.getConnection(function (err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("show Tables;");

            conn.query(strQuery, function (err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                } else {
                    console.log(rows);
                    res.render('jjaehuny/showTables', { type: "REAL SERVER", rows: rows, fields: fields });
                }
            })
        })
    }
};

exports.showTable = function(req, res){

    var server = req.param('server');
    console.log("server : " + server);

    var tableName = req.param('tableName');
    console.log(tableName);

    if (server == "Test")
    {
        var DBPool = req.app.get('Test_Game_Master');
        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("select * from %s;", tableName);
            console.log("query : " + strQuery);

            conn.query(strQuery, function (err, rows, fields) {
                if (err) {
                    console.log(err);
                } else {
                    console.log(rows);
                    res.render('jjaehuny/layout', { type: "TEST SERVER", rows: rows, fields: fields });
                }
                conn.release();
            })
        })
    }
    else {
        var DBPool = req.app.get('Real_Game_Backup');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("select * from %s;", tableName);

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                } else {
                    console.log(rows);
                    res.render('jjaehuny/layout', { type:"REAL SERVER",rows: rows, fields: fields });
                }
            })
        })
    }
};


exports.showTable_guild = function(req, res){

    var server = req.param('server');
    console.log("server : " + server);

    var tableName = req.param('tableName');
    console.log(tableName);

    if (server == "Test")
    {
        var DBPool = req.app.get('Test_Guild_Master');
        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("select * from %s;", tableName);
            console.log("query : " + strQuery);

            conn.query(strQuery, function (err, rows, fields) {
                if (err) {
                    console.log(err);
                } else {
                    console.log(rows);
                    res.render('jjaehuny/layout', { type: "TEST SERVER", rows: rows, fields: fields });
                }
                conn.release();
            })
        })
    }
    else {
        var DBPool = req.app.get('Real_Game_Backup');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("select * from %s;", tableName);

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                } else {
                    console.log(rows);
                    res.render('jjaehuny/layout', { type:"REAL SERVER",rows: rows, fields: fields });
                }
            })
        })
    }
};

exports.showConnection = function(req, res){

    var server = req.param('server');
    console.log(server);

    if (server == "Test")
    {
        console.log("Test");

        var DBPool = req.app.get('Test_Log_Master');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("select * from serveraddr_ccu;");

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                } else {
                    console.log(rows);
                    res.render('jjaehuny/layout', { type:"TEST SERVER",server: server, rows: rows, fields: fields });
                }
            })
        })
    }
    else
    {
        console.log("Real");

        var DBPool = req.app.get('Real_Log_Backup');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("select * from serveraddr_ccu;");

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                } else {
                    console.log(rows);
                    res.render('jjaehuny/layout', { type:"REAL SERVER",rows: rows, fields: fields });
                }
            })
        })
    }

};

exports.findFromSocialid = function(req, res){

    var server = req.param('server');
    console.log(server);

    var socialid = req.param('socialid');
    console.log(socialid);

    if (server == "Test")
    {
        console.log("Test");

        var DBPool = req.app.get('Test_Game_Master');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("select * from info_login where socialid = '%s';", socialid);

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                } else {
                    console.log(rows);
                    res.render('jjaehuny/layout', { type:"TEST SERVER",rows: rows, fields: fields });
                }
            })
        })
    }
    else
    {
        console.log("Real");

        var DBPool = req.app.get('Real_Game_Backup');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("select * from info_login where '%s';", socialid);

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                } else {
                    console.log(rows);
                    res.render('jjaehuny/layout', { type:"REAL SERVER",rows: rows, fields: fields });
                }
            })
        })
    }

};


exports.info_login_findFromseq = function(req, res){

    var server = req.param('server');
    console.log(server);

    var seq = req.param('seq');
    console.log(seq);

    if (server == "Test")
    {
        console.log("Test");

        var DBPool = req.app.get('Test_Game_Master');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("select * from info_login where seq = %s;", seq);

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                } else {
                    console.log(rows);
                    res.render('jjaehuny/layout', { type:"TEST SERVER",rows: rows, fields: fields });
                }
            })
        })
    }
    else
    {
        console.log("Real");

        var DBPool = req.app.get('Real_Game_Backup');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("select * from info_login where seq = %s;", seq);

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                } else {
                    console.log(rows);
                    res.render('jjaehuny/layout', { type:"REAL SERVER",rows: rows, fields: fields });
                }
            })
        })
    }

};

exports.info_user_findFromSocialid = function(req, res){

    var server = req.param('server');
    console.log(server);

    var socialid = req.param('socialid');
    console.log(socialid);

    if (server == "Test")
    {
        console.log("Test");

        var DBPool = req.app.get('Test_Game_Master');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("select * from info_user as a inner join info_login as b on a.user_seq = b.seq where b.socialid = '%s';", socialid);

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                } else {
                    console.log(rows);
                    res.render('jjaehuny/layout', { type:"TEST SERVER",rows: rows, fields: fields });
                }
            })
        })
    }
    else
    {
        console.log("Real");

        var DBPool = req.app.get('Real_Game_Backup');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("select * from info_user as a inner join info_login as b on a.user_seq = b.seq where b.socialid = '%s';", socialid);

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                } else {
                    console.log(rows);
                    res.render('jjaehuny/layout', { type:"REAL SERVER",rows: rows, fields: fields });
                }
            })
        })
    }
};




exports.findFromSeq = function(req, res){

    var server = req.param('server');
    console.log(server);

    var seq = req.param('seq');
    console.log(seq);

    if (server == "Test")
    {
        console.log("Test");

        var DBPool = req.app.get('Test_Game_Master');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("select * from info_user where user_seq = '%s';", seq);

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                } else {
                    console.log(rows);
                    res.render('jjaehuny/layout', { type:"TEST SERVER",rows: rows, fields: fields });
                }
            })
        })
    }
    else
    {
        console.log("Real");

        var DBPool = req.app.get('Real_Game_Backup');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("select * from info_user where user_seq = %s;", seq);
            console.log(strQuery);

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                } else {
                    console.log(rows);
                    res.render('jjaehuny/layout', { type:"REAL SERVER",rows: rows, fields: fields });
                }
            })
        })
    }
};



exports.findFromNickname = function(req, res){

    console.log("findFromNickname");

    var server = req.param('server');
    console.log(server);

    var nickname = req.param('nickname');
    console.log(nickname);

    if (server == "Test")
    {
        console.log("Test");

        var DBPool = req.app.get('Test_Game_Master');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("select * from info_user where nickname = '%s';", nickname);

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                } else {
                    console.log(rows);
                    res.render('jjaehuny/layout', { type:"TEST SERVER",rows: rows, fields: fields });
                }
            })
        })
    }
    else
    {
        console.log("Real");

        var DBPool = req.app.get('Real_Game_Backup');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("select * from info_user where nickname = '%s';", nickname);

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                } else {
                    console.log(rows);
                    res.render('jjaehuny/layout', { type:"REAL SERVER",rows: rows, fields: fields });
                }
            })
        })
    }

};







exports.update_env_config_block = function(req, res){

    console.log("findFromNickname");

    var server = req.param('server');
    console.log(server);

    var block = req.param('block');
    console.log(block);

    if (server == "Test")
    {
        console.log("Test");

        var DBPool = req.app.get('Test_Game_Master');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("update env_config set isBlock = %s;", block);

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                    res.render('jjaehuny/Result', { title: 'Express', result:"fail" });
                } else {
                    console.log(rows);
                    //alert("success");
                    res.render('jjaehuny/Result', { title: 'Express', result:"success" });
                }
            })
        })
    }
    else
    {
        console.log("Real");

        var DBPool = req.app.get('Real_Game_Master');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("update env_config set isBlock = %s;", block);

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                    res.render('jjaehuny/Result', { title: 'Express', result:"fail" });
                } else {
                    console.log(rows);
                    //alert("success");
                    res.render('jjaehuny/Result', { title: 'Express', result:"success" });
                }
            })
        })
    }

};


exports.update_resource_contents_open = function(req, res){

    console.log("update_resource_contents_open");

    var server = req.param('server');
    console.log(server);

    var contentString = req.param('content');
    console.log(contentString);

    var content = 0;
    if (contentString == 'vip')
        content = 0;

    var open = req.param('open');
    console.log(open);

    if (server == "Test")
    {
        console.log("Test");

        var DBPool = req.app.get('Test_Game_Master');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("update resource_contents_open set isOpen = %s where contents = %s;", open, content );

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                    res.render('jjaeuny/Result', { title: 'Express', result:"fail" });
                } else {
                    console.log(rows);
                    //alert("success");
                    res.render('jjaeuny/Result', { title: 'Express', result:"success" });
                }
            })
        })
    }
    else
    {
        console.log("Real");

        var DBPool = req.app.get('Real_Game_Master');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("update resource_contents_open set isOpen = %s where contents = %s;", open, content );

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                    //alert("fail");
                    res.render('jjaehuny/Result', { title: 'Express', result:"fail" });
                } else {
                    console.log(rows);
                    //alert("success");
                    res.render('jjaehuny/Result', { title: 'Express', result:"success" });
                }
            })
        })
    }

};


exports.getPVPInfo = function(req, res){

    console.log("get pvp info");

    var server = req.param('server');
    console.log(server);

    var seq = req.param('seq');
    console.log(seq);

    if (server == "Test")
    {
        console.log("Test");

        var DBPool = req.app.get('Test_Game_Master');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("select score, heart_blocked from info_user where seq = %s;", seq);

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                } else {
                    console.log(rows);
                    res.render('jjaehuny/layout', { type:"TEST SERVER",rows: rows, fields: fields });
                }
            })
        })
    }
    else
    {
        console.log("Real");

        var DBPool = req.app.get('Real_Game_Backup');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("select score, heart_blocked from info_user where seq = %s;", seq);

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    console.log(err);
                } else {
                    console.log(rows);
                    res.render('jjaehuny/layout', { type:"REAL SERVER",rows: rows, fields: fields });
                }
            })
        })
    }

};


exports.updateVIPPoint = function(req, res){

    console.log("updateVIPPoint");

    var server = req.param('server');
    console.log(server);

    var seq = req.param('seq');
    console.log(seq);

    var point = req.param('point');
    console.log(point);

    if (server == "Test")
    {
        console.log("Test");

        var DBPool = req.app.get('Test_Game_Master');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("update info_user set reserve4 = %s where user_seq = %s;", point, seq);
            console.log(strQuery);

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    res.render('jjaehuny/Result', { title: 'Express', result:"fail" });
                } else {
                    console.log(rows);
                    res.render('jjaehuny/Result', { title: 'Express', result:"success" });
                }
            })
        })
    }
    else
    {
        console.log("Real");

        var DBPool = req.app.get('Real_Game_Master');

        DBPool.getConnection(function(err, conn) {
            if (err) {
                throw err;
            }

            var strQuery = util.format("update info_user set reserve4 = %s where user_seq = %s;", point, seq);
            console.log(strQuery);

            conn.query(strQuery, function(err, rows, fields) {
                conn.release();
                if (err) {
                    res.render('jjaehuny/Result', { title: 'Express', result:"fail" });
                } else {
                    console.log(rows);
                    res.render('jjaehuny/Result', { title: 'Express', result:"success" });
                }
            })
        })
    }
};
